<?php
namespace app\referral\controller;

use think\Controller;
use tp5_lib\referral\logic\Referral;
use tp5_lib\common\controller\BaseController;

class IndexController extends BaseController
{
    /**
     * 返回推荐用户注册的链接
     */
    public function index()
    {
        $user=$this->checkLogin();
        $url=$this->request->domain().url("user/login/register");
        return Referral::url($url,$user);
    }
    /**
     * 直接返回二维码图片
     */
    public function qrcode()
    {
        $uid=input("uid");
        $dir=env("runtime_path")."/qrcode";
        if(!file_exists($dir)){
            mkdir($dir);
        }
        $path=$dir."referral-$uid";
        if(!file_exists($path)){
            require_once '../extend/phpqrcode.php';
            $domain=$this->request->domain();
            $url=$domain.url("user/login/register");
            $separator = strpos("?",$url) ===false ? "?" : "&";
            $url .= $separator."referral=".$uid;
            \QRcode::png($url,$path);
        }
        header("Content-type: image/png");
        readfile($path);
        exit;
    }
}
